package git

import (
	"bufio"
	"fmt"
	"log"
	"os/exec"
	"strings"
	"time"
)

type Repository struct {
	Path   string
	Branch string
}

const Commit = "Commit"
const Author = "Author"
const Date = "Date"
const Merge = "Merge"
const Comment = "Comment"

func (r *Repository) Branches() []string {
	script := fmt.Sprintf("cd %s && git branch -r", r.Path)
	log.Println("查询分支" + script)
	cmd := exec.Command("/bin/bash", "-c", script)
	output, err := cmd.StdoutPipe()

	if err != nil {
		_ = fmt.Errorf("执行命令失败,%s", err)
	}
	// 执行Linux命令
	if err := cmd.Start(); err != nil {
		fmt.Printf("Linux命令执行失败，请检查命令输入是否有误,%s \n", err.Error())

	}
	// 读取所有输出
	scanner := bufio.NewScanner(output)

	if err != nil {
		fmt.Printf("打印异常，请检查")
	}
	var result []string

	for {
		if !scanner.Scan() {
			break
		}
		line := scanner.Text()
		log.Println(line)
		result = append(result, "remotes/"+strings.TrimSpace(line))

	}
	return result
}

func (r *Repository) Logs2(branch string, author string, before string, since string, keyword string) []map[string]string {
	var gitScript string
	if branch == "" {
		branch = r.Branch
	}
	if author != "" {
		gitScript = fmt.Sprintf("git log %s --date=iso --author=%s ", branch, author)
	} else {
		gitScript = fmt.Sprintf("git log %s --date=iso ", branch)
	}
	if since != "" {
		gitScript = gitScript + " --since='" + since + "'"
	}
	if before != "" {
		gitScript = gitScript + " --before='" + before + "'"
	}

	if keyword != "" {
		gitScript = gitScript + " --grep='" + keyword + "'"
	}
	runScript := fmt.Sprintf("cd %s &&  %s", r.Path, gitScript)
	log.Println(runScript)
	result := exec.Command("/bin/bash", "-c", runScript)
	output, err := result.StdoutPipe()

	if err != nil {
		_ = fmt.Errorf("执行命令失败,%s", err)
	}
	// 执行Linux命令
	if err := result.Start(); err != nil {
		fmt.Printf("Linux命令执行失败，请检查命令输入是否有误,%s \n", err.Error())

	}
	// 读取所有输出
	scanner := bufio.NewScanner(output)

	if err != nil {
		fmt.Printf("打印异常，请检查")
	}

	var tmpLineName string
	var tmpValue string

	var logStruct []map[string]string
	var item map[string]string

	item = make(map[string]string)
	for {
		if !scanner.Scan() {
			break
		}
		line := scanner.Text()
		if 0 == strings.Index(line, "commit") {
			// 先将提交信息保存下来，如果有的话
			if tmpValue != "" {
				item[Comment] = tmpValue
				logStruct = append(logStruct, item)
				tmpValue = ""
			}
			item = make(map[string]string)
			tmpLineName = Commit
			item[Commit] = strings.TrimSpace(line[6:])
		}
		if 0 == strings.Index(line, "Author:") {
			tmpLineName = Author
			index := strings.Index(line, "<")
			item[Author] = strings.TrimSpace(line[7:index])
		}
		if 0 == strings.Index(line, "Date:") {
			tmpLineName = Date
			timeStr := strings.TrimSpace(line[5:])
			item[Date] = timeStr

		}
		if 0 == strings.Index(line, "Merge:") {
			tmpLineName = Merge
			item[Merge] = line
		} else if tmpLineName == Date {
			if 0 == strings.Index(line, "Date:") {
				continue
			}
			tmpValue = tmpValue + line
		}

	}
	return logStruct

}

func timeParse(timeStr string) {
	timeLayout := "2021-05-18 12:00:00"
	location, _ := time.LoadLocation("Local")
	localTime, _ := time.ParseInLocation(timeLayout, timeStr, location)
	fmt.Println(localTime.UTC().Format(timeLayout))
}
